package com.study.javaOther.dailytest;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@State(Scope.Benchmark)
public class ThreadTest {

    public static final int INT = 24;
    ExecutorService executorService = Executors.newFixedThreadPool(120);
    ExecutorService executorService1= Executors.newFixedThreadPool(12000);

    @TearDown(Level.Trial)
    public void tearDown(){
        System.out.println("TearDown");
        executorService.shutdown();
        executorService1.shutdown();

    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .warmupIterations(3)
                .measurementIterations(5)
                .timeUnit(TimeUnit.MILLISECONDS)
                .forks(1)
                .mode(Mode.SampleTime)
                .include(ThreadTest.class.getSimpleName())
                .build();
        new Runner(opt).run();
    }
    @Benchmark
    public void test120() {
        List<CompletableFuture<Void>> e = new ArrayList<>();
        for (int i = 0; i < INT; i++) {
            e.add(CompletableFuture.runAsync(new TestTask(),executorService)) ;
        }
        CompletableFuture.allOf(e.toArray(new CompletableFuture[0])).join();
    }

    @Benchmark
    public void test12000() {
        List<CompletableFuture<Void>> e = new ArrayList<>();
        for (int i = 0; i < INT; i++) {
            e.add(CompletableFuture.runAsync(new TestTask(),executorService1)) ;
        }
        CompletableFuture.allOf(e.toArray(new CompletableFuture[0])).join();
    }

    static class TestTask implements Runnable{
        @Override
        public void run() {
            for (int i = 0; i < 100000; i++) {
            }
        }
    }
}
